package com.sudichina.sudichina.view.activity.wallet;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.reflect.TypeToken;
import com.sudichina.sudichina.Contains;
import com.sudichina.sudichina.MyApp;
import com.sudichina.sudichina.NET.Api;
import com.sudichina.sudichina.NET.NetUtils;
import com.sudichina.sudichina.NET.onRequestDataFinish;
import com.sudichina.sudichina.R;
import com.sudichina.sudichina.bean.Result;
import com.sudichina.sudichina.bean.Wallet;
import com.sudichina.sudichina.utils.Toastutils;
import com.sudichina.sudichina.view.activity.login.LoginActivity;
import com.sudichina.sudichina.view.activity.main.BaseActivity;

import java.io.IOException;
import java.lang.reflect.Type;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by ${杨伟乔} on 2017/3/7.
 * 邮箱: yangweiqiao@126.com
 * QQ: 463217558
 * 别人光鲜的背后或者有着太多不为人知的痛苦
 * 学习可以改变一切、 行动可以解决一切问题、
 */
public class WalletActivity extends BaseActivity implements onRequestDataFinish {


    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_right)
    TextView tvRight;
    @BindView(R.id.tv_my_assets)
    TextView tvMyAssets;
    @BindView(R.id.ll_rechange)
    LinearLayout llRechange;
    @BindView(R.id.ll_tixian)
    LinearLayout llTixian;
    @BindView(R.id.tv_available_balance)
    TextView tvAvailableBalance;
    @BindView(R.id.tv_acw_detail)
    TextView tvAcwDetail;
    @BindView(R.id.tv_acw_tixianzhong_number)
    TextView tvAcwTixianzhongNumber;
    @BindView(R.id.tv_cwn_baozhengjin)
    TextView tvCwnBaozhengjin;
    @BindView(R.id.ll_my_bank_card)
    LinearLayout llMyBankCard;
    @BindView(R.id.ll_anw_jinqingqidai)
    LinearLayout llAnwJinqingqidai;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        /**
         * 加载我的 钱包 的界面
         */
        setContentView(R.layout.activity_my_wallet);
        ButterKnife.bind(this);

        //检测是否登录
        LoginActivity.startMeAndInitIsLogin(this, Contains.MYWALL_EXTRA);


    }


    /**
     * 请求数据
     */
    private void initDatas() {
        //主要的是查询个人的资金
        //首先判断当前的用户是否登录

        if (MyApp.getCurrentUser() != null) {
            //开始查询用户的资金信息
            Type type = new TypeToken<Result<Wallet>>() {
            }.getType();
            NetUtils.requestData(Api.MY_ASSETS + MyApp.getCurrentUser().getId(), null, this, type, false);
        }


    }


    @Override
    protected void onResume() {
        super.onResume();
        initDatas();
    }


    @Override
    public void requestdataFinish(String catalog, Object bean) {


        //注意回传的类型是 地址+id
        Result<Wallet> result = (Result<Wallet>) bean;

        if (result.getCode() == 1) {

            Wallet acountInfo = result.getData();
            if (acountInfo != null) {

//
//                tvMyAssets;//可用余额
//                tvAvailableBalance ;//总资产
//                tvAcwTixianzhongNumber;//提现中
//                tvCwnBaozhengjin;//保证金
                /*
                可用余额
                 */
                if (acountInfo.getBalance() == null) {
                    tvMyAssets.setText("0");

                } else {
                    tvMyAssets.setText(acountInfo.getBalance());
                }
                if (acountInfo.getFrozenMoney() == null) {
                    tvCwnBaozhengjin.setText("0");
                } else if (acountInfo.getFrozenMoney() != null) {

                    tvCwnBaozhengjin.setText(acountInfo.getFrozenMoney());
                }

//                if (acountInfo.getBalance() == null) {
//                    //可用余额为0
//                    if (acountInfo.getFrozenMoney() == null) {
////                        tvMyAssets.setText("￥" + new BigDecimal(0).setScale(2, BigDecimal.ROUND_HALF_UP) + "\n我的资产");
////                        tvAvailableBalance.setText("￥" + new BigDecimal(0).setScale(2, BigDecimal.ROUND_HALF_UP) + "\n可用余额");
////                        privateTvWithdraw.setText("￥" + new BigDecimal(0).setScale(2, BigDecimal.ROUND_HALF_UP) + "\n冻结资金");
//
//                    } else if (acountInfo.getFrozenMoney() != null) {
//                        float frozen = StringUtils.toFloat(acountInfo.getFrozenMoney(), 0);
////
////                        tvMyAssets.setText("￥" + new BigDecimal(frozen).setScale(2, BigDecimal.ROUND_HALF_UP) + "\n我的资产");
////                        tvAvailableBalance.setText("￥" + new BigDecimal(0).setScale(2, BigDecimal.ROUND_HALF_UP) + "\n可用余额");
////                        privateTvWithdraw.setText("￥" + new BigDecimal(frozen).setScale(2, BigDecimal.ROUND_HALF_UP) + "\n冻结资金");
//
//
//                    }
//                } else if (acountInfo.getBalance() != null) {
//                    float balance = StringUtils.toFloat(acountInfo.getBalance(), 0);
//                    if (acountInfo.getFrozenMoney() == null) {
////                        tvMyAssets.setText("￥" + new BigDecimal(balance).setScale(2, BigDecimal.ROUND_HALF_UP) + "\n我的资产");
////                        tvAvailableBalance.setText("￥" + new BigDecimal(balance).setScale(2, BigDecimal.ROUND_HALF_UP) + "\n可用余额");
////                        privateTvWithdraw.setText("￥" + new BigDecimal(0).setScale(2, BigDecimal.ROUND_HALF_UP) + "\n冻结资金");
//                        tvMyAssets.setText(""+ new BigDecimal(balance).setScale(2, BigDecimal.ROUND_HALF_UP));
//                        tvAvailableBalance.setText(""+ new BigDecimal(balance).setScale(2, BigDecimal.ROUND_HALF_UP));
//                    } else if (acountInfo.getFrozenMoney() != null) {
//
//                        float frozen = StringUtils.toFloat(acountInfo.getFrozenMoney(), 0);
////                        BigDecimal b = new BigDecimal(balance + frozen);
////                        tvMyAssets.setText("￥" + new BigDecimal(balance + frozen).setScale(2, BigDecimal.ROUND_HALF_UP) + "\n我的资产");
////                        tvAvailableBalance.setText("￥" + new BigDecimal(balance).setScale(2, BigDecimal.ROUND_HALF_UP) + "\n可用余额");
////                        privateTvWithdraw.setText("￥" + new BigDecimal(frozen).setScale(2, BigDecimal.ROUND_HALF_UP) + "\n冻结资金");
//
//                    }
//                }

            }


            if (acountInfo == null) {
//                //设置所有的值为0
//                tvMyAssets.setText("￥" + new BigDecimal(0).setScale(2, BigDecimal.ROUND_HALF_UP) + "\n我的资产");
//                tvAvailableBalance.setText("￥" + new BigDecimal(0).setScale(2, BigDecimal.ROUND_HALF_UP) + "\n可用余额");
//                privateTvWithdraw.setText("￥" + new BigDecimal(0).setScale(2, BigDecimal.ROUND_HALF_UP) + "\n冻结资金");


            }
        } else if (result.getCode() == 3)

        {

            Toastutils.showToast(result.getMsg());
        }


    }

    @Override
    public void requestdataFailed(IOException e) {
        Toastutils.showToast("网络异常");

    }


    @OnClick({R.id.iv_back, R.id.ll_rechange, R.id.ll_tixian, R.id.tv_acw_detail, R.id.ll_my_bank_card, R.id.ll_anw_jinqingqidai})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.ll_rechange:
                /**
                 * 跳转充值界面
                 */
                Intent intent = new Intent(this, RechargeActivity.class);
                startActivity(intent);


                break;
            case R.id.ll_tixian:
                break;
            case R.id.tv_acw_detail:
                break;
            case R.id.ll_my_bank_card:
                break;
            case R.id.ll_anw_jinqingqidai:
                break;
        }
    }
}
